<?php
namespace Shared\LRC\TwigExtensions;

/**
 * @package LRCSupport
 * @version 1.0.0.0
 * @author J.Minchev
 * @internal 
 * @todo
 */
class LRCSupport {

    private $EntityManager;

    public function __construct(\Doctrine\ORM\EntityManager $em) {
        $this->EntityManager = $em;
    }

    public function getLRCProperties($Entity) {
        $Parser = new \Shared\LRC\Metadata\Parsers\Entity($Entity, $this->EntityManager);
        return json_encode($Parser->parse()->getLrcProperties());
    }

    public function getLRCContentJSON($Entity) {
        return json_encode($this->getLRCContent($Entity));
    }

    public function getLRCContent($Entity) {
        $Parser = new \Shared\LRC\Metadata\Parsers\Entity($Entity, $this->EntityManager);
        $EntityMetadata = $Parser->parse();

        $TranslationParser = new \Shared\LRC\Metadata\Parsers\Translation($EntityMetadata->getTranslationEntityClass(), $this->EntityManager);
        $TranslationMetadata = $TranslationParser->parse();

        $lrcContent = array();
        $translations = $Entity->{'get' . ucfirst($EntityMetadata->getTranslationPropertyName())}();
        foreach ($translations as $Translation) {
            foreach ($EntityMetadata->getLrcProperties() as $property) {
                $Language = $Translation->{'get' . ucfirst($TranslationMetadata->getLanguagePropertyName())}();
                $lrcContent[$Language->getId()][$property] = $Translation->{'get' . ucfirst($property)}();
            }
        }
        return $lrcContent;
    }

    public function getLanguages() {
        return $this->EntityManager->getRepository('Shared\LRC\Entity\Language')->findAll();
    }

}